import threading
import time

import threadpool


def sayhello(name):
    print("%s say Hello to %s" % (threading.current_thread().getName(), name));
    time.sleep(1)
    return name


success = 0


def callback(request, result):  # 回调函数，用于取回结果
    print(request.args[0])
    globals()['success'] += 1
    print("init" + str(globals()['success']))
    print("callback result = %s" % result)


name_list = ['admin', 'root', 'scott', 'tiger']
start_time = time.time()
pool = threadpool.ThreadPool(10)  # 创建线程池
requests = threadpool.makeRequests(sayhello, name_list, callback)  # 创建任务
[pool.putRequest(req) for req in requests]  # 加入任务

pool.wait()
print('%s cost %d second' % (threading.current_thread().getName(), time.time() - start_time))
